package com.example.nextstream.api;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.nextstream.annotations.AuthRequired;
import com.example.nextstream.models.AssetsInfo;
import com.example.nextstream.models.User;
import com.example.nextstream.service.AssetsInfoService;
import com.example.nextstream.utils.RspBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/assets")
public class AssetsApi {
    @Autowired
    private AssetsInfoService assetsInfoService;


    @RequestMapping("createAsset")
    @AuthRequired
    @CrossOrigin
    public JSONObject createAsset(@RequestParam("assetsName") String assetsName,
                                  @RequestParam("assetsIp") String assetsIp,
                                  @RequestParam("assetsProtocol") String assetsProtocol,
                                  @RequestParam("assetsPort") Integer assetsPort,
                                  @RequestParam("assetsUsername") String assetsUsername,
                                  @RequestParam("assetsPassword") String assetsPassword,
                                  @RequestParam("privateKey") String privateKey,
                                  @RequestParam("passphrase") String passphrase) {
        AssetsInfo assets = new AssetsInfo();
        assets.setAssetsIp(assetsIp);
        assets.setAssetsName(assetsName);
        assets.setAssetsUsername(assetsUsername);
        assets.setAssetsPort(assetsPort);
        assets.setAssetsPassword(assetsPassword);
        assets.setAssetsProtocol(assetsProtocol.toLowerCase());
        assets.setPrivateKey(privateKey);
        assets.setPassphrase(passphrase);
        if (!(assetsProtocol.equalsIgnoreCase("rdp") ||
                assetsProtocol.equalsIgnoreCase("ssh") ||
                assetsProtocol.equalsIgnoreCase("vnc") || assetsProtocol.equalsIgnoreCase("telnet"))) {
            return RspBuilder.formatFailResult("not support protocol");
        }
        assetsInfoService.createAssets(assets);
        return RspBuilder.createSuccessRsp().build();
    }

    @RequestMapping("listAssets")
    @AuthRequired
    @CrossOrigin
    public JSONObject listAsset(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {

        Page<AssetsInfo> res = assetsInfoService.listAssetsInfo(pageNum, pageSize);
        return RspBuilder.createSuccessRsp().append("total", res.getTotal()).append("assets", res.getRecords()).build();
    }

    @RequestMapping("count")
    @AuthRequired
    @CrossOrigin
    public JSONObject countAssets() {
        Integer count = assetsInfoService.countAssets();
        return RspBuilder.createSuccessRsp().append("count", count).build();
    }

    @RequestMapping("deleteAsset")
    @AuthRequired
    @CrossOrigin
    public JSONObject deleteAsset(@RequestParam("assetId") Integer assetId) {
        assetsInfoService.deleteAssets(assetId);
        return RspBuilder.createSuccessRsp().build();
    }
}
